package com.itheima.leyou.web;

import com.itheima.leyou.po.Brand;
import com.itheima.leyou.po.SpecGroup;
import com.itheima.leyou.po.SpecParam;
import com.itheima.leyou.service.SpecGroupService;
import com.itheima.leyou.service.SpecParamService;
import com.leyou.item.dto.SpecGroupDTO;
import com.leyou.item.dto.SpecParamDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/spec")
public class SpecController {
    @Autowired
    private SpecGroupService specGroupService;

    @Autowired
    private SpecParamService specParamService;

    /**
     * 根据商品分类id，查询规格组的集合
     * @param id
     * @return
     */
    @GetMapping("/groups/of/category")
    public ResponseEntity<List<SpecGroupDTO>> querySpecGroupByCategory(
            @RequestParam("id")Integer id){
        return ResponseEntity.ok(specGroupService.querySpecGroupByCategory(id));
    }


    /**
     * 根据分类id查询规格组及组内参数
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    public ResponseEntity<List<SpecGroupDTO>> listSpecGroup(
            @RequestParam("id")Long categoryId){
        return ResponseEntity.ok(specGroupService.listSpecGroup(categoryId));
    }


    /**
     * 根据条件查询规格参数集合
     * @param categoryId
     * @return
     */
    @GetMapping("/params")
    public ResponseEntity<List<SpecParamDTO>> querySpecParam(
            @RequestParam(value = "categoryId",required = false)Long categoryId,
            @RequestParam(value = "groupId",required = false)Long groupId,
            @RequestParam(value = "searching",required = false)Boolean searching
            ){
        return ResponseEntity.ok(specParamService.querySpecParam(categoryId,groupId,searching));
    }


    /**
     * 新增规格组信息
     * @param groupDTO
     * @return
     */
    @PostMapping("/group")
    public ResponseEntity<Void> addGroup(@RequestBody SpecGroupDTO groupDTO){
         specGroupService.addGroup(groupDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }


    /**
     * 修改规格组信息
     * @param groupDTO
     * @return
     */
    @PutMapping ("/group")
    public ResponseEntity<Void> updateGroup(@RequestBody SpecGroupDTO groupDTO){
        this.specGroupService.updateGroup(groupDTO);
        return ResponseEntity.ok(null);
    }

    /**
     * 新增规格参数信息
     * @param paramDTO
     * @return
     */
    @PostMapping("/param")
    public ResponseEntity<Void> addParam(@RequestBody SpecParamDTO paramDTO){
        specParamService.addParam(paramDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 修改规格参数信息
     * @param paramDTO
     * @return
     */
    @PutMapping ("/param")
    public ResponseEntity<Void> updateParam(@RequestBody SpecParamDTO paramDTO){
        this.specParamService.updateParam(paramDTO);
        return ResponseEntity.ok(null);
    }

}
